package ui;

import java.awt.Dimension;
import java.util.Iterator;
import java.util.Set;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.statistics.HistogramDataset;
import org.jfree.data.xy.IntervalXYDataset;
import org.jfree.ui.ApplicationFrame;

import entity.Image;

public class HistogramVisualization extends ApplicationFrame{

	//-----Gerando Histograma-------------------------
	public HistogramVisualization(String s, Image image)  
	{  
		super(s);
		IntervalXYDataset intervalxydataset = createDataset(image);  
		JFreeChart jfreechart = createChart(intervalxydataset);  
		ChartPanel chartpanel = new ChartPanel(jfreechart);  
		chartpanel.setPreferredSize(new Dimension(500, 270));
		setContentPane(chartpanel);
		
	}

	private static IntervalXYDataset createDataset(Image image) {
		double[] histograma = new double[image.getHeight()*image.getWidth()];
		int count=0;
		Set<Integer> keys = image.getHistogram().getHistogram().keySet();
		for (Iterator iterator = keys.iterator(); iterator.hasNext();) {
			int key = (Integer) iterator.next();
			for (int i = 0; i < image.getHistogram().getHistogram().get(key); i++) {
				histograma[count] = key;
				count++;
			}
		}

		HistogramDataset histogramdataset = new HistogramDataset();
		histogramdataset.addSeries("Histograma", histograma, image.getMaxValue());
		return histogramdataset;
	}

	private JFreeChart createChart(IntervalXYDataset intervalxydataset)  
	{  
		JFreeChart jfreechart = ChartFactory.createHistogram("Histograma", null, null, intervalxydataset, PlotOrientation.VERTICAL, true, true, true);  
		jfreechart.getXYPlot().setForegroundAlpha(0.75F);  
		return jfreechart;  
	}
}
